package org.hl7.fhir.r5.test.profiles.codegen;

import java.util.List;
import java.util.ArrayList;
import javax.annotation.Nullable;
import java.util.Date;


import org.hl7.fhir.r5.context.IWorkerContext;
import org.hl7.fhir.r5.model.*;
import org.hl7.fhir.r5.profilemodel.PEBuilder;
import org.hl7.fhir.r5.profilemodel.PEInstance;
import org.hl7.fhir.r5.profilemodel.PEBuilder.PEElementPropertiesPolicy;
import org.hl7.fhir.r5.profilemodel.gen.PEGeneratedBase;
import org.hl7.fhir.r5.profilemodel.gen.Min;
import org.hl7.fhir.r5.profilemodel.gen.Max;
import org.hl7.fhir.r5.profilemodel.gen.Label;
import org.hl7.fhir.r5.profilemodel.gen.Doco;
import org.hl7.fhir.r5.profilemodel.gen.BindingStrength;
import org.hl7.fhir.r5.profilemodel.gen.ValueSet;
import org.hl7.fhir.r5.profilemodel.gen.MustSupport;
import org.hl7.fhir.r5.profilemodel.gen.Definition;


// Generated by the HAPI Java Profile Generator, 5/11/24, 6:00 pm

/**
 * An ingredient of a manufactured item or pharmaceutical product.
 *
 */
public class IngredientUvEpi extends PEGeneratedBase {

  public static final String CANONICAL_URL = "http://hl7.org/fhir/uv/emedicinal-product-info/StructureDefinition/Ingredient-uv-epi|1.0.0";

  public enum PublicationStatus {
    DRAFT, // "Draft" = http://hl7.org/fhir/publication-status#draft
    ACTIVE, // "Active" = http://hl7.org/fhir/publication-status#active
    RETIRED, // "Retired" = http://hl7.org/fhir/publication-status#retired
    UNKNOWN; // "Unknown" = http://hl7.org/fhir/publication-status#unknown

    public static PublicationStatus fromCode(String s) {
      switch (s) {
      case "draft": return DRAFT;
      case "active": return ACTIVE;
      case "retired": return RETIRED;
      case "unknown": return UNKNOWN;
      default: return null;
      }
    }

    public static PublicationStatus fromCoding(Coding c) {
      if ("http://hl7.org/fhir/publication-status".equals(c.getSystem()) && "draft".equals(c.getCode())) {
        return DRAFT;
      }
      if ("http://hl7.org/fhir/publication-status".equals(c.getSystem()) && "active".equals(c.getCode())) {
        return ACTIVE;
      }
      if ("http://hl7.org/fhir/publication-status".equals(c.getSystem()) && "retired".equals(c.getCode())) {
        return RETIRED;
      }
      if ("http://hl7.org/fhir/publication-status".equals(c.getSystem()) && "unknown".equals(c.getCode())) {
        return UNKNOWN;
      }
      return null;
    }

    public static PublicationStatus fromCodeableConcept(CodeableConcept cc) {
      for (Coding c : cc.getCoding()) {
        PublicationStatus v = fromCoding(c);
        if (v != null) {
          return v;
        }
      }
      return null;
    }

    public String toDisplay() {
      switch (this) {
      case DRAFT: return "Draft";
      case ACTIVE: return "Active";
      case RETIRED: return "Retired";
      case UNKNOWN: return "Unknown";
      default: return null;
      }
    }

    public String toCode() {
      switch (this) {
      case DRAFT: return "draft";
      case ACTIVE: return "active";
      case RETIRED: return "retired";
      case UNKNOWN: return "unknown";
      default: return null;
      }
    }

    public Coding toCoding() {
      switch (this) {
      case DRAFT: return new Coding().setSystem("http://hl7.org/fhir/publication-status").setCode("draft");
      case ACTIVE: return new Coding().setSystem("http://hl7.org/fhir/publication-status").setCode("active");
      case RETIRED: return new Coding().setSystem("http://hl7.org/fhir/publication-status").setCode("retired");
      case UNKNOWN: return new Coding().setSystem("http://hl7.org/fhir/publication-status").setCode("unknown");
      default: return null;
      }
    }

    public CodeableConcept toCodeableConcept() {
      Coding c = toCoding();
      return c == null ? null : new CodeableConcept().addCoding(c);
    }
  }

  @Min("0") @Max("1") @Doco("")
  private String id;  // 

  @Min("0") @Max("*") @Doco("Additional content defined by implementations")
  @Definition("May be used to represent additional information that is not part of the basic definition of the resource. To make the use of extensions safe and managable, there is a strict set of governance applied to the definition and use of extensions. Though any implementer can define an extension, there is a set of requirements that SHALL be met as part of the definition of the extension.")
  private List<Extension> extensions = new ArrayList<>();  // Additional content defined by implementations

  @Min("0") @Max("*") @Doco("Extensions that cannot be ignored")
  @Definition("May be used to represent additional information that is not part of the basic definition of the resource and that modifies the understanding of the element that contains it and/or the understanding of the containing element's descendants. Usually modifier elements provide negation or qualification. To make the use of extensions safe and managable, there is a strict set of governance applied to the definition and use of extensions. Though any implementer is allowed to define an extension, there is a set of requirements that SHALL be met as part of the definition of the extension. Applications processing a resource are required to check for modifier extensions.\n\nModifier extensions SHALL NOT change the meaning of any elements on Resource or DomainResource (including cannot change the meaning of modifierExtension itself).")
  private List<Extension> modifierExtensions = new ArrayList<>();  // Extensions that cannot be ignored

  @Min("0") @Max("1") @Doco("An identifier or code by which the ingredient can be referenced.")
  @Definition("The identifier(s) of this Ingredient that are assigned by business processes and/or used to refer to it when a direct URL reference to the resource itself is not appropriate.")
  private Identifier identifier;  // An identifier or code by which the ingredient can be referenced.

  @Min("1") @Max("1") @Doco("draft|active|retired|unknown")
  @BindingStrength("required") @ValueSet("http://hl7.org/fhir/ValueSet/publication-status|5.0.0")
  @Definition("The status of this ingredient. Enables tracking the life-cycle of the content.")
  private PublicationStatus status;// @NotNull  // draft|active|retired|unknown

  @Min("0") @Max("*") @Doco("The product which this ingredient is a constituent part of")
  @Definition("The product which this ingredient is a constituent part of.")
  private List<Reference> _fors = new ArrayList<>();  // The product which this ingredient is a constituent part of

  @Min("1") @Max("1") @Doco("Purpose of the ingredient within the product, e.g. active, inactive")
  @BindingStrength("example") @ValueSet("http://hl7.org/fhir/uv/emedicinal-product-info/ValueSet/ingredientRole")
  @Definition("A classification of the ingredient identifying its purpose within the product, e.g. active, inactive.")
  private CodeableConcept role;// @NotNull  // Purpose of the ingredient within the product, e.g. active, inactive

  @Min("0") @Max("1") @Doco("If the ingredient is a known or suspected allergen.")
  @Definition("If the ingredient is a known or suspected allergen. Note that this is a property of the substance, so if a reference to a SubstanceDefinition is used to decribe that (rather than just a code), the allergen information should go there, not here.")
  private boolean allergenicIndicator;  // If the ingredient is a known or suspected allergen.

  @Min("0") @Max("*") @Doco("An organization that manufactures this ingredient")
  @Definition("The organization(s) that manufacture this ingredient. Can be used to indicate:         1) Organizations we are aware of that manufacture this ingredient         2) Specific Manufacturer(s) currently being used         3) Set of organisations allowed to manufacture this ingredient for this product         Users must be clear on the application of context relevant to their use case.")
  private List<BackboneElement> manufacturers = new ArrayList<>();  // An organization that manufactures this ingredient

  @Min("1") @Max("1") @Doco("The substance that comprises this ingredient")
  @Definition("The substance that comprises this ingredient.")
  private BackboneElement substance;// @NotNull  // The substance that comprises this ingredient


  /**
   * Parameter-less constructor.
   *
   */
  public IngredientUvEpi() {
  }

  /**
   * Construct an instance of the object, and fill out all the fixed values 
   *
   */
  public IngredientUvEpi(IWorkerContext context) {
    workerContext = context;
    PEBuilder builder = new PEBuilder(context, PEElementPropertiesPolicy.EXTENSION, true);
    PEInstance src = builder.buildPEInstance(CANONICAL_URL, builder.createResource(CANONICAL_URL, false));
    load(src);
  }

  /**
   * Populate an instance of the object based on this source object 
   *
   */
  public static IngredientUvEpi fromSource(IWorkerContext context, Ingredient source) {
    IngredientUvEpi theThing = new IngredientUvEpi();
    theThing.workerContext = context;
    PEBuilder builder = new PEBuilder(context, PEElementPropertiesPolicy.EXTENSION, true);
    PEInstance src = builder.buildPEInstance(CANONICAL_URL, source);
    theThing.load(src);
    return theThing;
  }


  public void load(PEInstance src) {
    clear();
    if (src.hasChild("id")) {
      id = ((IdType) src.child("id").asDataType()).getValue();
    }
    for (PEInstance item : src.children("extension")) {
      extensions.add((Extension) item.asDataType());
    }
    for (PEInstance item : src.children("modifierExtension")) {
      modifierExtensions.add((Extension) item.asDataType());
    }
    if (src.hasChild("identifier")) {
      identifier = (Identifier) src.child("identifier").asDataType();
    }
    if (src.hasChild("status")) {
      status = PublicationStatus.fromCode(src.child("status").asDataType().primitiveValue());
    }
    for (PEInstance item : src.children("for")) {
      _fors.add((Reference) item.asDataType());
    }
    if (src.hasChild("role")) {
      role = (CodeableConcept) src.child("role").asDataType();
    }
    if (src.hasChild("allergenicIndicator")) {
      allergenicIndicator = ((BooleanType) src.child("allergenicIndicator").asDataType()).getValue();
    }
    for (PEInstance item : src.children("manufacturer")) {
      manufacturers.add((BackboneElement) item.asElement());
    }
    if (src.hasChild("substance")) {
      substance = (BackboneElement) src.child("substance").asElement();
    }

  }

  /**
   * Build a instance of the underlying object based on this wrapping object 
   *
   */
  public Ingredient build(IWorkerContext context) {
    workerContext = context;
    return build();
  }

  /**
   * Build a instance of the underlying object based on this wrapping object 
   *
   */
  public Ingredient build() {
    Ingredient theThing = new Ingredient();
    PEBuilder builder = new PEBuilder(workerContext, PEElementPropertiesPolicy.EXTENSION, true);
    PEInstance tgt = builder.buildPEInstance(CANONICAL_URL, theThing);
    save(tgt, false);
    return theThing;
  }

  /**
   * Save this profile class into an existing resource (overwriting anything that 
   * exists in the profile) 
   *
   */
  public void save(IWorkerContext context, Ingredient dest, boolean nulls) {
    workerContext = context;
    PEBuilder builder = new PEBuilder(context, PEElementPropertiesPolicy.EXTENSION, true);
    PEInstance tgt = builder.buildPEInstance(CANONICAL_URL, dest);
    save(tgt, nulls);
  }

  public void save(PEInstance tgt, boolean nulls) {
    tgt.clear("id");
    if (id != null) {
      tgt.makeChild("id").data().setProperty("value", new IdType(id));
    }
    tgt.clear("extension");
    for (Extension item : extensions) {
      tgt.addChild("extension", item);
    }
    tgt.clear("modifierExtension");
    for (Extension item : modifierExtensions) {
      tgt.addChild("modifierExtension", item);
    }
    tgt.clear("identifier");
    if (identifier != null) {
      tgt.addChild("identifier", identifier);
    }
    tgt.clear("status");
    if (status != null) {
      tgt.addChild("status", status.toCode());
    }
    tgt.clear("for");
    for (Reference item : _fors) {
      tgt.addChild("for", item);
    }
    tgt.clear("role");
    if (role != null) {
      tgt.addChild("role", role);
    }
    tgt.clear("allergenicIndicator");
    if (true) { // for now, at least
      tgt.makeChild("allergenicIndicator").data().setProperty("value", new BooleanType(allergenicIndicator));
    }
    tgt.clear("manufacturer");
    for (BackboneElement item : manufacturers) {
      tgt.addChild("manufacturer", item);
    }
    tgt.clear("substance");
    if (substance != null) {
      tgt.addChild("substance", substance);
    }

  }

  public String getId() {
    return id;
  }

  public IngredientUvEpi setId(String value) {
    this.id = value;
    return this;
  }

  public boolean hasId() {
    return id != null;
  }

  /**
   * May be used to represent additional information that is not part of the basic 
   * definition of the resource. To make the use of extensions safe and managable, 
   * there is a strict set of governance applied to the definition and use of 
   * extensions. Though any implementer can define an extension, there is a set of 
   * requirements that SHALL be met as part of the definition of the extension.
   *
   */
  public List<Extension> getExtensions() {
    if (extensions == null) { extensions = new ArrayList<>(); }
    return extensions;
  }

  public boolean hasExtensions() {
    return extensions != null && !extensions.isEmpty();
  }

  public Extension addExtension() {
    Extension theThing = new Extension();
    getExtensions().add(theThing);
    return theThing;
  }

  public boolean hasExtension(Extension item) {
    return hasExtensions() && extensions.contains(item);
  }

  public void removeExtension(Extension item) {
    if (hasExtension(item)) {
      extensions.remove(item);
    }
  }


  /**
   * May be used to represent additional information that is not part of the basic 
   * definition of the resource and that modifies the understanding of the element 
   * that contains it and/or the understanding of the containing element's 
   * descendants. Usually modifier elements provide negation or qualification. To 
   * make the use of extensions safe and managable, there is a strict set of 
   * governance applied to the definition and use of extensions. Though any 
   * implementer is allowed to define an extension, there is a set of requirements 
   * that SHALL be met as part of the definition of the extension. Applications 
   * processing a resource are required to check for modifier extensions.
   *
   * 
   *
   * Modifier extensions SHALL NOT change the meaning of any elements on Resource or 
   * DomainResource (including cannot change the meaning of modifierExtension 
   * itself).
   *
   */
  public List<Extension> getModifierExtensions() {
    if (modifierExtensions == null) { modifierExtensions = new ArrayList<>(); }
    return modifierExtensions;
  }

  public boolean hasModifierExtensions() {
    return modifierExtensions != null && !modifierExtensions.isEmpty();
  }

  public Extension addModifierExtension() {
    Extension theThing = new Extension();
    getModifierExtensions().add(theThing);
    return theThing;
  }

  public boolean hasModifierExtension(Extension item) {
    return hasModifierExtensions() && modifierExtensions.contains(item);
  }

  public void removeModifierExtension(Extension item) {
    if (hasModifierExtension(item)) {
      modifierExtensions.remove(item);
    }
  }


  /**
   * The identifier(s) of this Ingredient that are assigned by business processes 
   * and/or used to refer to it when a direct URL reference to the resource itself is 
   * not appropriate.
   *
   */
  public Identifier getIdentifier() {
    if (identifier == null) { identifier = new Identifier(); }
    return identifier;
  }

  public IngredientUvEpi setIdentifier(Identifier value) {
    this.identifier = value;
    return this;
  }
  public boolean hasIdentifier() {
    return identifier != null;
  }

  /**
   * The status of this ingredient. Enables tracking the life-cycle of the content.
   *
   */
  public PublicationStatus getStatus() {
    return status;
  }

  public IngredientUvEpi setStatus(PublicationStatus value) {
    this.status = value;
    return this;
  }

  public boolean hasStatus() {
    return status != null;
  }

  /**
   * The product which this ingredient is a constituent part of.
   *
   */
  public List<Reference> get_fors() {
    if (_fors == null) { _fors = new ArrayList<>(); }
    return _fors;
  }

  public boolean has_fors() {
    return _fors != null && !_fors.isEmpty();
  }

  public Reference add_for() {
    Reference theThing = new Reference();
    get_fors().add(theThing);
    return theThing;
  }

  public boolean has_for(Reference item) {
    return has_fors() && _fors.contains(item);
  }

  public void remove_for(Reference item) {
    if (has_for(item)) {
      _fors.remove(item);
    }
  }


  /**
   * A classification of the ingredient identifying its purpose within the product, 
   * e.g. active, inactive.
   *
   */
  public CodeableConcept getRole() {
    if (role == null) { role = new CodeableConcept(); }
    return role;
  }

  public IngredientUvEpi setRole(CodeableConcept value) {
    this.role = value;
    return this;
  }
  public boolean hasRole() {
    return role != null;
  }

  /**
   * If the ingredient is a known or suspected allergen. Note that this is a property 
   * of the substance, so if a reference to a SubstanceDefinition is used to decribe 
   * that (rather than just a code), the allergen information should go there, not 
   * here.
   *
   */
  public boolean getAllergenicIndicator() {
    return allergenicIndicator;
  }

  public IngredientUvEpi setAllergenicIndicator(boolean value) {
    this.allergenicIndicator = value;
    return this;
  }

  public boolean hasAllergenicIndicator() {
    return true; // not allergenicIndicator != false ?
  }

  /**
   * The organization(s) that manufacture this ingredient. Can be used to indicate:   
   *       1) Organizations we are aware of that manufacture this ingredient         
   * 2) Specific Manufacturer(s) currently being used         3) Set of organisations 
   * allowed to manufacture this ingredient for this product         Users must be 
   * clear on the application of context relevant to their use case.
   *
   */
  public List<BackboneElement> getManufacturers() {
    if (manufacturers == null) { manufacturers = new ArrayList<>(); }
    return manufacturers;
  }

  public boolean hasManufacturers() {
    return manufacturers != null && !manufacturers.isEmpty();
  }

  public boolean hasManufacturer(BackboneElement item) {
    return hasManufacturers() && manufacturers.contains(item);
  }

  public void removeManufacturer(BackboneElement item) {
    if (hasManufacturer(item)) {
      manufacturers.remove(item);
    }
  }


  /**
   * The substance that comprises this ingredient.
   *
   */
  public @Nullable BackboneElement getSubstance() { // BackboneElement is abstract 
    return substance;
  }

  public IngredientUvEpi setSubstance(BackboneElement value) {
    this.substance = value;
    return this;
  }
  public boolean hasSubstance() {
    return substance != null;
  }



  public void clear() {
    id = null;
    extensions.clear();
    modifierExtensions.clear();
    identifier = null;
    status = null;
    _fors.clear();
    role = null;
    allergenicIndicator = false;
    manufacturers.clear();
    substance = null;

  }

}
